import time

import requests

# 1.1登录鉴权接口
url = "http://192.168.204.134:38000//"
def seafile_login(username="seafile@admin.com",password="admin"):
    api = "api2/auth-token/"
    header = {"content-type":"application/x-www-form-urlencoded"}
    body = {"username":username,"password":password}
    resp = requests.post(url+api,headers=header,data=body)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 200 == resp.status_code
    assert "token" in resp.text
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp
# 1.2获取账号信息
def seafile_get_all_info(token):
    api = "api2/account/info/"
    header = {"Authorization":"Token "+token}
    resp = requests.get(url+api,headers=header)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 200 == resp.status_code
    assert "seafile@admin.com" == resp.json().get("email")
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp
# 1.3新增一个资料库接口
def seafile_add_repos(token):
    api = "api2/repos/"
    header = {"Authorization":"Token "+token}
    body = {"name":f"test{int(time.time()*1000)}"}
    resp = requests.post(url+api,headers=header,data=body)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 200 == resp.status_code
    assert resp.json().get("repo_id") is not None
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp
# 1.4查询所有的资料库接口
def seafile_all_repos(token,repo_id):
    api = "api2/repos/"
    header = {"Authorization":"Token "+token}
    # body = {"name":f"test{int(time.time()*1000)}"}
    resp = requests.get(url+api,headers=header)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 200 == resp.status_code
    assert repo_id == resp.json()[0].get("id")
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp
# 1.5 修改资料库名字
def seafile_update_repos(token,repo_id,repo_name):
    api = f"/api2/repos/{repo_id}/?op=rename"
    header = {"Authorization":"Token "+token}
    body = {"repo_name":f"{repo_name}"}
    resp = requests.post(url+api,headers=header,data=body)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 200 == resp.status_code
    assert "success" == resp.json()
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp

# 1.6 删除指定的资料库接口
def seafile_delete_repos(token,repo_id):
    api = f"api2/repos/{repo_id}/"
    header = {"Authorization":"Token "+token}
    # body = {"name":f"test{int(time.time()*1000)}"}
    resp = requests.delete(url+api,headers=header)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 200 == resp.status_code
    assert "success" == resp.json()
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp
# 1.7在指定的资料库目录新建文件接口
def seafile_add_file(token,repo_id,repo_name):
    api = f"api2/repos/{repo_id}/file/?p=/{repo_name}&reloaddir=true"
    header = {"Authorization":"Token "+token}
    body = {"operation":"create"}
    resp = requests.post(url+api,headers=header,data=body)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 200 == resp.status_code
    assert resp.json()[0].get("name") is not None
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp
# 1.8在指定的资料库目录删除文件接口
def seafile_delete_file(token,repo_id,repo_name):
    api = f"api2/repos/{repo_id}/file/?p=/{repo_name}&reloaddir=true"
    header = {"Authorization":"Token "+token}
    # body = {"operation":"create"}
    resp = requests.delete(url+api,headers=header)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 200 == resp.status_code
    assert [] == resp.json()
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp
# 1.9添加星标文件
def seafile_add_starredfiles(token,repo_id,repo_name):
    api = "api2/starredfiles/"
    header = {"Authorization":"Token "+token}
    body = {"repo_id":repo_id,"p":f"/{repo_name}"}
    resp = requests.post(url+api,headers=header,data=body)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 201 == resp.status_code
    assert "success" == resp.json()
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp
# 1.10查询星标文件
def seafile_get_starredfiles(token,repo_name):
    api = "api2/starredfiles/"
    header = {"Authorization":"Token "+token}
    # body = {"repo_id":repo_id,"p":f"/{repo_name}"}
    resp = requests.get(url+api,headers=header)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 200 == resp.status_code
    assert str(repo_name) == resp.json()[0].get("file_name")
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp
# 1.11删除星标文件
def seafile_delete_starredfiles(token,repo_id,repo_name):
    api = f"api2/starredfiles/?repo_id={repo_id}&p=/{repo_name}"
    header = {"Authorization":"Token "+token}
    body = {"repo_id":repo_id,"p":f"/{repo_name}"}
    resp = requests.delete(url+api,headers=header,data=body)
    # print(f"响应状态码：{resp.status_code}")
    # print(f"响应文本：{resp.text}")
    # print(f"响应头：{resp.headers}")
    # print(f"响应时间：{resp.elapsed.total_seconds()}")
    assert 200 == resp.status_code
    assert "success" == resp.json()
    assert 0.2 >= resp.elapsed.total_seconds()
    return resp